package com.spt.ws.tasks.impl;

import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.spt.core.Property;
import com.spt.core.TaskAnnotation;
import com.spt.util.DateHelper;
import com.spt.ws.client.WebServiceBase;
import com.spt.ws.dao.IClientDao;
import com.spt.ws.tasks.ITask;

@Component(value = "qgxxSendBean")
@TaskAnnotation(job_name = "齐格信息", task_id = "FjjToWy_qgxx", group_id = "fjj_to_wy", cron = "0/4 * * * * ?", autoRun = "N")
public class QgxxTask extends WebServiceBase implements ITask {
    private static Logger log = LogManager.getLogger("com.spt.ws.client");
    @Autowired
    private IClientDao dao;

    private String head = "#HEAD";
    private String end = "#END";

    @Override
    public void doTask() {
        log.info("start qgxx（齐格信息）..........");
        try {
            List<Map<String, Object>> li = dao.getQGFF();
            // StringBuffer parmyy = new StringBuffer(head);
            StringBuffer parm = new StringBuffer(head);
            String[] headkeys = Property.getProperty("fjj.send.qgff.head").split(",");
            String[] bodykeys = Property.getProperty("fjj.send.qgff.body").split(",");
            if (headkeys == null || headkeys.length == 0 || bodykeys == null || bodykeys.length == 0) {
                // log.error("记取字段配置信息出错");
                return;
            }
            if (li == null || li.size() == 0) {
                log.info("qgxx（齐格信息） no data selected,retrun!");
                return;
            }
            for (Map<String, Object> map : li) {
                // parmyy.append(this.assembleLine(keysyy, map));
                parm.append(this.assembleLine(headkeys, map));
                // ids.append(map.get("ID")).append(",");
                List<Map<String, Object>> libody = (List<Map<String, Object>>) map.get("maillist");
                if (libody == null){
                    parm.setLength(0);
                    parm.append(head);
                    continue;
                }

                for (Map<String, Object> mapbody : libody) {
                    parm.append(this.assembleLine(bodykeys, mapbody));
                }
                parm.append(end);
                log.info("qgxx（齐格信息） send msg is : " + parm.toString());
                String res = callWS("getQGXX", parm.toString());
                log.info("qgxx（齐格信息） return msg is :" + res);
                // map.put("FSSJ", DateHelper.getDateTime());
                if (checkMsg(res)) {
                    dao.updateQGFF(map, "1");
                } else {
                    dao.updateQGFF(map, "9");
                }
                parm.setLength(0);
                parm.append(head);
            }
            li.clear();
        } catch (Exception e) {
            log.info("qgxx（齐格信息） error: " + e.getMessage());
        }
        log.info("end qgxx（齐格信息）..........");
    }

}
